package service.impl;

import com.github.pagehelper.PageHelper;
import mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Article;
import service.IArticleService;

import java.util.List;

/**
 * 用户帖子的服务层
 */
@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Article getArticleById(int id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        return article;
    }

    @Override
    public List<Article> getArticlesByUserId(int userId) {
        List<Article> articles = articleMapper.selectByUserId(userId);
        return articles;
    }

    @Override
    public int addArticle(Article article) {
        int i = articleMapper.insertSelective(article);
        return i;
    }

    @Override
    public List<Article> getArticlesByTop(int pageSize) {
        //获取置顶的前limit条帖子
        PageHelper.startPage(1, pageSize);
        List<Article> articles = articleMapper.selectByTop();
        return articles;
    }

    @Override
    public List<Article> getArticlesByBuildDate(int pageCurrent, int pageSize) {
        PageHelper.startPage(pageCurrent, pageSize);
        List<Article> articles = articleMapper.selectByBuildDateAsc();
        return articles;
    }

    @Override
    public int modifyArticleClickNumber(Article article) {
        int i = articleMapper.updateByPrimaryKeySelective(article);
        return i;
    }

    @Override
    public int modifyArticleCommentNumber(Article article) {
        int i = articleMapper.updateByPrimaryKeySelective(article);
        return i;
    }

    @Override
    public List<Article> getArticlesByCommentNumber(int pageCurrent, int pageSize) {
        PageHelper.startPage(pageCurrent, pageSize);
        List<Article> articles = articleMapper.selectByCommentNumber();
        return articles;
    }

    @Override
    public List<Article> getArticlesByUnsolved(int pageCurrent, int pageSize) {
        PageHelper.startPage(pageCurrent, pageSize);
        List<Article> articles = articleMapper.selectByOver(0);
        return articles;
    }

    @Override
    public int getArticleCount() {
        int count = articleMapper.selectAllCount();
        return count;
    }

    @Override
    public List<Article> getArticlesBySolved(int pageCurrent, int pageSize) {
        PageHelper.startPage(pageCurrent,pageSize );
        List<Article> articles = articleMapper.selectByOver(1);
        return articles;
    }

    @Override
    public List<Article> getArticlesByCategoryId(int categoryId, int pageCurrent, int pageSize) {
        PageHelper.startPage(pageCurrent,pageSize );
        List<Article> articles = articleMapper.selectByCategoryId(categoryId);
        return articles;
    }

    @Override
    public int getArticleCountByCategoryId(int categoryId) {
        int count = articleMapper.selectCountByCategoryId(categoryId);
        return count;
    }

    @Override
    public int getArticleCountBySolved(int solved) {
        int count = articleMapper.selectCountByOver(solved);
        return count;
    }

    @Override
    public List<Article> getArticlesByMany(Article article, int pageCurrent, int pageSize) {
        PageHelper.startPage(pageCurrent, pageSize);
        List<Article> articles = articleMapper.selectByMany(article);
        return articles;
    }

    @Override
    public int getArticleCountByMany(Article article) {
        int count = articleMapper.selectCountByMany(article);
        return count;
    }

    @Override
    public int modifyArticleByArticleId(Article article) {
        int count = articleMapper.updateByPrimaryKeySelective(article);
        return count;
    }
}
